library(xtable)
library(tidyverse)
library(stargazer)
library(data.table)
library(haven)
library(wordcloud)
library(ggpubr)

#Table B.1: Summary Statistics of Revolving-Door Officials vs Others####
df = read_rds( "~/Dropbox/revolving_door_jmp/replication_files/summary_stats.rds")
# function of summary stats
treat = df %>% 
  filter(rd_official == 1) %>% 
  select(-rd_official)

treat = map_df(treat, function(x) {
  tibble(N=length(na.omit(x)), Mean=mean(x,na.rm = T), `St. Dev.`=sd(x,na.rm = T))
})

control = df %>% 
  filter(rd_official == 0) %>% 
  select(-rd_official)

control = map_df(control, function(x) {
  tibble(N=length(na.omit(x)), Mean=mean(x,na.rm = T), `St. Dev.`=sd(x,na.rm = T))
})

# t-test
a = df %>%
  dplyr::select(-rd_official) %>% 
  map(~t.test(.x ~ df$rd_official, data = df)) 
a[[1]]$stderr
a %>% map("estimate") %>% map(2) %>% unlist()

vis = tibble(
  RD_Officials =  a %>% map("estimate") %>% map(2) %>% unlist() %>% round(3),
  Others = a %>% map("estimate") %>% map(1) %>% unlist()  %>% round(3),
  Dif =  a %>% map("estimate") %>% map(2) %>% unlist()  %>% round(3) -  a %>% map("estimate") %>% map(1) %>% unlist() %>% round(3),
  P_value =  a %>% map_dbl( "p.value") %>% round(3)
)  
vis

var_name = c("Female","Age","Years of Education", 
             "CPC Member","People Congress Member", 
             "CPPCC Member","Length of Tenure", "Salary (in logs)", 
             "Stock Asset (in logs)")

bind_cols(  Variable = var_name,
            treat,control,vis %>% select( Dif, P_value)
) %>%
  tibble() %>% 
  xtable()

# Table B.2: Position Breakdown of Revolvers####
rd_all = read_csv("~/Dropbox/revolving_door_jmp/replication_files/rd_all_rep.csv") %>% filter(!is.na(prov))
rd_official = read_csv("~/Dropbox/revolving_door_jmp/replication_files/rd_official_rep.csv")
rd_all_0719 = read_csv("~/Dropbox/revolving_door_jmp/replication_files/rd_all_0719.csv")

pos_sum =  rd_all %>%
  filter(personid %in%  rd_all_0719$personid) %>% 
  mutate(pos_clear_en = paste(pos_clear, pos_clear_en," ")) %>% 
  select(pos_clear_en)

questionr::freq(pos_sum$pos_clear_en, cum = TRUE, sort = "dec", total = TRUE) %>% 
  as.data.frame()  %>%
  dplyr::select("n" ,"%","%cum") %>% 
  xtable(caption = "Position Breakdown of RD Officials",
         label = "tab:pos_breakdown")

# Table B.3: Department Breakdown of Revolvers####
dpt_sum =  rd_all %>%
  filter(personid %in%  rd_all_0719$personid) %>% 
  mutate(dpt_cn_en = paste(department, department_en," ")) %>% 
  select(dpt_cn_en)

questionr::freq(dpt_sum$dpt_cn_en, cum = TRUE, sort = "dec", total = TRUE) %>% 
  as.data.frame()  %>%
  dplyr::select("n" ,"%","%cum") %>% 
  xtable(caption = "Department Breakdown of RD Officials",
         label = "tab:dept_breakdown")

# Figure B.3: Word Cloud of the Department Branches of Revolving-Door Officials####
Sys.getlocale("LC_CTYPE")
par(family='STKaiti')

wrd_rd_background = rd_official %>% 
  group_by(org_suffix1) %>%
  tally() %>% 
  arrange(desc(n)) %>% 
  filter(!is.na(org_suffix1)) %>% 
  head(100)

set.seed(2) # for reproducibility 

#pdf("/Users/zerenli1992/Dropbox/Apps/Overleaf/subsidies_for_sale_2020/figure/word_cloud.png")
wordcloud(words = wrd_rd_background$org_suffix1, freq = wrd_rd_background$n, min.freq = 4,
          max.words=100, random.order=FALSE, rot.per=0.3, 
          colors=brewer.pal(8, "Dark2"))

#dev.off() 

#Figure B.4: Career Paths of Revolving-Door Officials, 2007–2019####
library(igraph)
library(data.table)
library(igraph)
library(RColorBrewer)
rd_official = read_csv("~/Dropbox/revolving_door_jmp/replication_files/rd_official_rep.csv") 

career_path = rd_official %>%
  ungroup()  %>%
  dplyr::select(personid, org,org_suffix1,type,sentence_seq) %>%
  mutate(org = group_indices(., org,org_suffix1)) %>% 
  filter(type!= "other") %>% 
  mutate(type = fct_infreq(type)) %>% 
  #select(-org) %>% 
  #rename(org = org_suffix1) %>% 
  as.data.table() %>% 
  arrange(personid, sentence_seq) %>% 
  group_by(personid) %>% 
  mutate(next_org = lead(org))

levels(career_path$type)

edge = career_path %>% 
  dplyr::select(next_org, org) %>% 
  filter(is.na(next_org) == F)  %>% 
  group_by(next_org, org)  %>% 
  summarise(n = n())


vertex = career_path %>% 
  ungroup() %>%
  group_by(org,type) %>% 
  tally() %>% 
  ungroup() %>% 
  mutate(type = as.factor(type))  %>% 
  mutate(type = fct_reorder(type,n)) 

# convert as an igraph object
net <- graph_from_data_frame(edge, vertex, directed=TRUE) 
edge.start <- ends(net, es=E(net), names=F)[,1]
edge.col <- V(net)$color[edge.start]

coul  <- c( "#FC8D62" ,"#66C2A5" ,"#8DA0CB" ,"#E78AC3", "#A6D854" ,"#FFD92F")

# Create a vector of color
#V(net)$type = ordered(V(net)$type, levels = c("Government", " Party", "Military","Court&Procuratorate","People's Congress&CPPCC", "Youth League"))
my_color <- coul[as.numeric(as.factor(V(net)$type))]

V(net)$type %>% table()


# add edge weight
E(net)$width <- log(E(net)$n+1)
V(net)$size <- log(V(net)$n+1)*2


# plot network graph
network = plot(net,
               layout=layout_nicely,
               edge.arrow.size= .1, 
               vertex.frame.width  = .2,
               vertex.label          = NA,
               vertex.label.col      =  "black",
               vertex.label.fontface = "bold",
               vertex.label.show     = .5,
               vertex.label.range    = c(10, 25),
               vertex.color = my_color,
               edge.color = "lightgray"
)

legend(x=-1.3,
       y.intersp = .5,
       y= -.5,## position, also takes x,y coordinates
       c("Court&Procuratorate","Government", "Military", "Party","People's Congress&CPPCC", "Youth League"),
       pch = 19,   
       cex=1.2,## legend symbols see ?points
       col = coul,
       bty = "n",
       ncol=1)


#Figure B.5: Revolvers vs Other Directors/Executives ####
tibble(
  coef =  a %>% map("estimate") %>% map(2) %>% unlist()  %>% round(3) -  a %>% map("estimate") %>% map(1) %>% unlist() %>% round(3),
  se =  a %>% map_dbl( "stderr") %>% round(3),
  type = ordered(c(rep("Demographic",3),rep("Political",3),rep("Firm",3)),levels = c("Demographic", "Political", "Firm")),
  var = ordered(var_name,levels = rev(var_name)),
)  %>% 
  bind_cols(Variable = var_name) %>%   
  ggplot()+
  geom_hline(yintercept = 0, color = "gray", linetype = "dashed",size =.5) +
  geom_pointrange(aes(y = coef, min=coef - 1.96 * se, max=coef + 1.96* se ,x = var), lwd=.3) +
  geom_pointrange(aes(y = coef, min=coef - 1.64 * se, max=coef + 1.64* se ,x = var), lwd=.5 ) +
  theme_classic(15) +#color = type,,shape = type
  coord_flip() +
  theme(legend.title = element_blank(),
        #legend.position = "none",
        legend.text = element_text(size = 12),
        #    axis.text.x = element_blank(),
        axis.ticks = element_blank(),
        #          line = element_blank()
  ) +
  ylab("RD Officials vs Other Directors/Executives") +
  xlab("")

ggsave("/Users/zerenli1992/Dropbox/Apps/Overleaf/subsidies_for_sale_2020/figure/rd_other_com.png", width = 9,height = 4)
